#!/bin/bash

function usage() {
    echo "Usage: $( basename $0 ) [ -c <local_dir> -l <log_file>] <git_url>"
    echo "    git clone <git_url> 到 <local_dir>"
    echo "    参数说明:"
    echo "    -c <local_dir>: 本地保存目录, 缺省为当前目录"
    echo "    -l <log_file>: 日志文件,缺省为/dev/stdout"
    echo "    <git_url>: git url"
    exit
}

set -e
log=/tmp/cron.log

sCnt=0
localDir=$( pwd )
logFile="/dev/stdout"
while getopts ":c:l:" args
do
    case "${args}" in 
        c)
            sCnt=$(( ${sCnt} + 2 ))
            localDir="${OPTARG}"
            ;;
        l)
            sCnt=$(( ${sCnt} + 2 ))
            logFile="${OPTARG}"
            ;;
        *)
            usage
            ;;
    esac
done

shift ${sCnt}
gitUrl="$1"
if [ -z "${gitUrl}" ]
then 
    usage
fi

echo "git clone started at $(date) in ${localDir}" >> "${logFile}"
git -C "${localDir}" clone "${gitUrl}" >> "${logFile}"
echo "git clone end at $(date)" >> "${logFile}"
